from mongoengine import *


class ContentType(Document):
    cid = StringField(max_length=200, primary_key=True)
    _class = StringField(max_length=100)
    name = StringField(max_length=200)
    createDate = DateTimeField()
    fId = StringField(max_length=200)
    position = IntField(max_length=200)
    fName = StringField(max_length=200)
    pic = StringField(max_length=400)
    isDeleted = BooleanField()
    isSystemType = BooleanField()
    floor = IntField(max_length=2)
    weight = IntField(max_length=32)
    createUser = StringField(max_length=200)
    extendPropAndVal = ListField(DictField())
    contents = ListField(StringField(max_length=50))
    isCsType = BooleanField()
    meta = {'collection': 'contentType', 'db_alias': '202'}

    def getRemenTypeSql(self):
        return "SELECT t1.id, t1.CategoryName , t1.AddTime, t1.SortNum, CASE WHEN t1.FId = '0' THEN 'knowledge_huaiyun' ELSE t1.FId END fid, CASE WHEN t2.CategoryName IS NULL THEN '怀孕' ELSE t2.CategoryName END fname, t1.CreateUser, t1.IconPic FROM KnowledgeType t1 LEFT JOIN KnowledgeType t2 ON t1.fid = t2.id WHERE t1.type = 2;"

    def proRemenType(self, ret):
        arr = []
        return ContentType(cid=ret[0], _class='cn.ibabygroup.cms.domainmodels.ContentType', name=ret[1],
                           createDate=ret[2], fId=ret[4], fName=ret[5], pic=ret[7], isDeleted=False, isSystemType=False,
                           weight=ret[3], createUser=ret[6], contents=arr, isCsType=True)


def query(key):
    return ContentType.objects(name=key)
